package com.lanou3g.demo.cost.controller;

import com.lanou3g.demo.cost.service.ICostService;
import com.lanou3g.demo.entity.Cost;
import com.lanou3g.demo.mapper.CostMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@Controller
public class CostController {
    @Autowired
    private ICostService costService;

    @RequestMapping("/fee_list1")
    @ResponseBody
    public List<Cost> findAll(int start, int count) {
        return costService.findAll(start, count);
    }

    @RequestMapping("/fee_addOne")
    public String insertOne(@Validated Cost cost, BindingResult result, Model model) {
        if (result.hasErrors()) {
            model.addAttribute("result", result);
        } else {
            costService.insertOne(cost);
        }
        return "redirect:/fee/fee_list.jsp";
    }

    @RequestMapping("/fee_updateOne")
    public String updateOne(Cost cost) {
        costService.updateOne(cost);
        return "redirect:/fee/fee_list.jsp";
    }

    @RequestMapping("/fee_startOne")
    public void stsrtOne(Cost cost) {
        cost.setStartime(new Timestamp(System.currentTimeMillis()));
        costService.stsrtOne(cost);
    }

    @RequestMapping("/fee_deleteOne")
    public void deleteOne(Cost cost) {
        costService.deleteOne(cost);
    }

    @RequestMapping("/fee_list_page")
    @ResponseBody
    public Map findTotal(HttpServletRequest request) {
        Map<String, Integer> message = costService.findTotal();
        if (request.getParameter("currentPage") != null) {
            Integer currentPage1 = Integer.valueOf(request.getParameter("currentPage"));
            message.put("currentPage", currentPage1);
        }
        System.out.println(request.getParameter("currentPage"));
        return message;
    }
}
